package dk.hojlind.hackhunt.model;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class IPGetter {

	//Public variables

	//Private variables

	public IPGetter()
	{

	}

	public List<String> getIp()
	{
		List<String> adresses = new ArrayList<String>();
		try
		{
		Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
		while (interfaces.hasMoreElements())
		{
			NetworkInterface current = interfaces.nextElement();
			if (!current.isUp()) 
			{
				continue;
			}
			Enumeration<InetAddress> addresses = current.getInetAddresses();

			while (addresses.hasMoreElements())
			{
				InetAddress current_addr = addresses.nextElement();                
				if(current_addr.toString().contains(":"))
				{
					//do nothing
				}
				else if(current_addr.toString().contains("127."))
				{
					//do nothing
				}
				else
				{
					adresses.add(current_addr.getHostAddress());
				}
			}            
		}
		}
		
		catch(SocketException e)
		{
			adresses.add("Could not find IP adress");
		}

		return adresses;
	}

}
